// @include "../../../scripts/spket.js"

(function() {
	var log = panda.log("proxy.validation");

	proxy.validation = { priority: 60 };

	proxy.validation.saveUser = {
		priority: 100,
		expr: /^dbo.users.(add|update)$/,
		func: function(name, method, args) {
			var validated = validator.validateUser(args[0]);

			if (!validated.success) {
				log.info(validated.error);
				throw validated.error;
			}

			args[0] = validated.data;

			return this[method].apply(this, args);
		}
	};

	proxy.validation.addUser = {
		priority: 80,
		expr: /^dbo.users.add$/,
		func: function(name, method, args) {
			var user = args[0];

			if (this.exists(user.name)) {
				var msg = "The user already exists.";
				log.info(msg);
				throw msg;
			}

			return this[method].apply(this, args);
		}
	};
}());
